<?php

/**
 * Created by PhpStorm.
 * User: zhangxiang
 * Date: 2017/6/2 0002
 * Time: 下午 4:33
 */
class ArticleListModel
{
    private $articleListKeyPrefix;
    private $articleListCacheModel;
    private $articleBodyModel;
    private $autoUidModel;
    private $voteModel;

    public function __construct(){
        $this->articleListKeyPrefix = 'ARL/';
        $this->articleListCacheModel = new ArticleListCacheModel();
        $this->articleBodyModel     =   new ArticleBodyModel();
        $this->autoUidModel         =   new AutoUidModel();
        $this->voteModel            =   new VoteModel();
    }

    public function addArticleList($param)
    {
        $param['updated_at']   =   AC::SERVER_TIME;
        $param['created_at']   =   AC::SERVER_TIME;
        $key    =   $this->articleListKeyPrefix. $param['uid'];
        $result =   Storage::addData($key, $param);
        if (empty($result))
        {
            AC::$errorNo    =   'ALM002';
            return  FALSE;
        }
        return  $result;
    }

    /**
     * 获取文章数据（若无缓存则添加）
     * @param $articleBodyId
     * @param $author_uid
     * @return array|bool|null
     */
    public function getArticleInfoByArticleBodyId($articleBodyId, $author_uid)
    {
        if (empty($articleBodyId)) return array();
        $articleInfo = $this->articleListCacheModel->getArticleCache($articleBodyId);

        if(!empty($articleInfo) && intval($articleInfo['status']) == 0 && intval($articleInfo['last_time']) > 0){

            isset($articleInfo['count']) && $articleInfo['count'] = UK::parseData($articleInfo['count']);
            isset($articleInfo['attachment']) && $articleInfo['attachment'] = UK::parseData($articleInfo['attachment']);
            isset($articleInfo['modules']) && $articleInfo['modules'] = UK::parseData($articleInfo['modules']);
            isset($articleInfo['extras']) && $articleInfo['extras'] = UK::parseData($articleInfo['extras']);

            return $articleInfo;
        }
        $articleInfo = $this->getArticleListInfo($author_uid, $articleBodyId);
        if (empty($articleInfo) || intval($articleInfo['status']) != 0) {
            AC::$errorNo    = 'ALM001';
            AC::$errorMsg   = '文章不存在或已被删除';
            return NULL;
        }
        $articleItemInfo = $this->articleBodyModel->getArticleBody($author_uid, $articleBodyId);

        if(empty($articleItemInfo) || intval($articleItemInfo['status']) != 0) {
            return NULL;
        }
        $articleInfo['content'] = $articleItemInfo['content'];
        $articleDetail =  $this->changeArticleListItem($articleInfo);

        $this->articleListCacheModel->addArticleCache($articleBodyId, $articleDetail);

        return $articleDetail;
    }

    public function getArticleListInfo($uid, $articleBodyId){
        $where  =   array('aid' => $articleBodyId, 'uid' => $uid);
        $key    =   $this->articleListKeyPrefix. $uid;
        $result =   Storage::getConditionData($key, $where);
        return  $result;
    }

    // 检查文章格式
    private function changeArticleListItem($data){
        $articleInfo = array();
        $articleInfo['id']             =    strval($data['id']);
        $articleInfo['uid']            =    strval($data['uid']);
        $articleInfo['aid']            =    strval($data['aid']);
        $articleInfo['title']          =    empty($data['title'])    ? '' : $data['title'];
        $articleInfo['cover']          =    empty($data['cover'])    ? '' : $data['cover'];
        $articleInfo['summary']        =    empty($data['summary'])    ? '' : $data['summary'];
        $articleInfo['status']         =    strval($data['status']);
        $articleInfo['category']       =    empty($data['category']) ? '0' : $data['category'];
        $articleInfo['category']       =    $this->articleBodyModel->articleCategory[$articleInfo['category']];
        $articleInfo['content']        =    empty($data['content']) ? '' : $data['content'];
        $articleInfo['vote']           =    empty($data['vote']) ? '' : $data['vote'];
        $articleInfo['diaryId']        =    empty($data['int1']) ? '0' :  $data['int1'];
        $articleInfo['time']           =    strval($data['created_at']);
        $articleInfo['created_at']     =    strval($data['created_at']);
        $articleInfo['updated_at']      =    strval($data['updated_at']);

        $articleInfo['repostTimes']    =    strval($data['repostTimes']);
        $articleInfo['viewTimes']      =    strval($data['viewTimes']);
        $articleInfo['commentTimes']   =    strval($data['commentTimes']);
        $articleInfo['likeTimes']      =    strval($data['likeTimes']);
        $articleInfo['shareTimes']     =    strval($data['shareTimes']);
        $articleInfo['last_time']      =    AC::SERVER_TIME;
        $articleInfo['original']       =    strval($data['int2']);
        if (!empty($data['extras'])) {
            $extras = UK::parseData($data['extras']);
            $articleInfo['modules'] = $extras['modules'];
            $articleInfo['attachment'] = $extras['attachment'];
            $articleInfo['tag'] = $extras['tag'];
            if($data['int2'] != 1 && isset($extras['source'])) {
                $articleInfo['original'] = $extras['source'];
            }
        }
//        $articleInfo['modules']['vote'] = $this->voteModel->getVoteInfo($data['aid'], $data['uid'], $data['vote'], AC::HOST_UID);
        $articleInfo['extras']  =  $data['extras'];

        return $articleInfo;
    }

    /**
     * 获取用户列表
     * @param $uid
     * @param $id
     * @param $length
     * @return array|bool
     */
    public function getUserArticleList($uid, $id, $length)/*{{{*/
    {
        if (empty($uid))
        {
            AC::$errorNo    =   'ALM003';
            return  FALSE;
        }
        $where  =   ' uid = ' . $uid . ' and status = 0 ';
        if (!empty($id))   $where  .=   'and id < '. $id;

        $start  =    0;
        $length =   !empty($length) ? $length : AC::DIARY_LIST_DEFAULT_LEN;
        $key    =   $this->articleListKeyPrefix. $uid;
        $orderBy=   ' id DESC ';
        if ($start  <   0)  $start  =   0;

        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);

        return  $result;
    }

    /**
     * 删除列表数据
     * @param $uid
     * @param $articleBodyId
     * @return bool|int|resource
     */
    public function delArticleList($uid, $articleBodyId)
    {
        if (empty($uid) ||  empty($articleBodyId))
        {
            AC::$errorNo    =   'ALM004';
            return  FALSE;
        }
        $data['updated_at'] =   AC::SERVER_TIME;
        $data['status']     =   1;
        $where  =   array('aid' => $articleBodyId, 'uid' => $uid, 'status' => 0);
        $key    =   $this->articleListKeyPrefix. $uid;
        $result =   Storage::setConditionData($key, $data, $where);
        return  $result;
    }

    /**
     * 更新列表
     * @param $param
     * @return bool|int|resource
     */
    public function updateArticleList($param)
    {
        $param['updated_at'] =   AC::SERVER_TIME;
        $where  =   array('aid' => $param['aid'], 'uid' => $param['uid'], 'status' => 0);
        $key    =   $this->articleListKeyPrefix. $param['uid'];
        $result =   Storage::setConditionData($key, $param, $where);
        return $result;
    }

    /**
     * 列表
     * @param $type
     * @param $id
     * @param $length
     * @return array|bool
     */
    public function getArticleList($type, $id, $length)
    {
        list($where, $orderBy) = $this->getArticleListSql($type, $id);

        $start  =    0;
        $length =   isset($length) ?   $length:   AC::DIARY_LIST_DEFAULT_LEN;
        $key    =   $this->articleListKeyPrefix. AC::HOST_UID;

        if ($start  <   0)  $start  =   0;
        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
        return  $result;
    }

    private function getArticleListSql($type, $id = 0)
    {
        switch ($type) {
            case "host":
                $orderBy=   ' likeTimes DESC ';
                break;
            default:
                $orderBy=   ' id DESC ';
                break;
        }
        $where  =   ' status = 0 ';
        if (!empty($id))   $where  .=   'and id < '. $id;
        return [$where, $orderBy];
    }

}